﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.BaseManageForms.SamplingPlaceForms;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.SamplingPlaceMixForms
{
    public partial class SamplingPlaceMixMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.SamplingPlaceMixBll placeMixBll = new BLL.SamplingPlaceMixBll();
        BLL.ReportMixBll reportMixBll = new BLL.ReportMixBll();
        public SamplingPlaceMixMainForm()
        {
            InitializeComponent();
        }

        private void SamplingPlaceMixMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            InitGrid();
        }

        private void InitGrid()
        {
            var models = placeMixBll.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.SamplingPlaceMixID).ToList();
            this.gridControl1.DataSource = models;
        }

        private void barLargeButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SamplingPlaceMixDetailForm form = new SamplingPlaceMixDetailForm(InitGrid);
            form.Show();
        }

        private void barLargeButtonItem_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string id = this.gridView1.GetFocusedRowCellValue("SamplingPlaceMixID").ToString();
            SamplingPlaceMix palce = placeMixBll.LoadEntities(m => m.SamplingPlaceMixID == id).FirstOrDefault();
            if (palce != null)
            {
                SamplingPlaceMixDetailForm form = new SamplingPlaceMixDetailForm(palce, InitGrid);
                form.Show();
            }
        }

        private void barLargeButtonItem_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                string id = this.gridView1.GetFocusedRowCellValue("SamplingPlaceMixID").ToString();
                SamplingPlaceMix palce = db.SamplingPlaceMixes.Where(m => m.SamplingPlaceMixID == id).FirstOrDefault();
                int count = reportMixBll.LoadEntitiesAsNoTracking(m => m.SamplingPlaceMixID == palce.SamplingPlaceMixID).Count();
                if (count == 0)
                {
                    db.SamplingPlaceMixes.Remove(palce);
                    bool res = db.SaveChanges() > 0;
                    if (res)
                    {
                        XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                        InitGrid();
                    }
                }
                else
                {
                    XtraMessageBox.Show(ResourceOverall.DeleteExists);
                }
            }
        }



    }
}